package com.li.jiang.utils.token;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.li.jiang.obj.dto.UserLoginData;

import java.util.UUID;

public class TokenUtil {

     final static ObjectMapper  objectMapper = new ObjectMapper();
    /**
     * 生成JWTData
     *
     * @param userLoginData
     * @return tokenStr
     */
    public static String createTokenData(UserLoginData userLoginData)  {
        try {
            return objectMapper.writeValueAsString(userLoginData);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 解析JWTData数据
     * @param tokenStr
     * @return userLoginData
     */
    public static UserLoginData parseTokenData(String tokenStr){
        try {
            return objectMapper.readValue(tokenStr, UserLoginData.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 创建JWT
     * @return userLoginData
     */
    public static String createToken(){

         return UUID.randomUUID().toString().replace("-","");
    }

}
